﻿
var vm = new Vue({
    el: '#app',
    data: function () {
        return {
            pagename: "生产派工",
            common_fn: null,
            userinfo: null,
            loading: false,
            maxScreenHeight: 0,
            mainTable_maxHeight: 0,
            //班组
            team: "",
            // 所在车间所有设备列表
            devicesByTeam: [],
            //班组下员工
            peopleByTeam: [],
            orderData: [],//订单
            selectedOrderID: "",
            selectedOrderNo: "",
            selectedProductioScheduleID: "",//选中的订单对应的批次计划ID
            assignsheets: [],  //开工列表
            assignsheetsLength: 0,//开工列表长度
            multipleSelection: [],
            claimworkLoading: false,
            assign_index: 0,//选中开工条目index
            assign_row: "",//选中开工条目row
            claimworkDialog: false,//开工对话框
            claimWork: {
                startNumber: '',//开工数量
            },
        }
    },
    methods: {
        //看图
        retColor: function (item) {
            if (item.column.label == '图号' || item.column.label == '批次号') { return 'viewDrawing' }
        },
        cell_click: function (row, column, cell) {
            var self = this;
            if (column.label == '图号') {
                //
            }
        },
        //多选
        handleSelectionChange: function (val) {
            console.log(val)
        },
        // 获取所有订单
        getOrders: function () {
            var self = this;
            self.loading = true;
            var filters = [{
                "classFlag": 1,
                "classType": "MesOrderSheet",
                "propName": "state",
                "op": 4,
                "propValue": "1",      //已排产发布的订单
                "propValue2": "",
                "logic": 0,
                "leftSplitter": 0,
                "rightSplitter": 0,
                "bAddWhereClauseFlag": true
            }];
            self.common_fn.queryObject("MesOrderSheet", filters, self.userinfo, function (response) {
                if (response.resFlag == 0) {
                    for (i = response.result.length - 1; i >= 0; i--) {
                        response.result[i].properties.state == 0 ? response.result[i].properties['state_name'] = '待审核' : response.result[i].properties.state == 1 ? response.result[i].properties['state_name'] = '审核中' : response.result[i].properties['state_name'] = '已发布';
                        response.result[i].properties.deliverydate = response.result[i].properties.deliverydate.split(' ')[0];
                        response.result[i].properties.creationdate = response.result[i].properties.creationdate.split(' ')[0]; //APP加
                        response.result[i].properties.orderquantity = Number(response.result[i].properties.orderquantity);
                        self.orderData.push({
                            "id": response.result[i].id,
                            "name": response.result[i].properties.name,
                        })
                    }
                    self.orderData = response.result;
                    if (self.orderData.length !== 0) {
                        self.selectedOrderNo = self.orderData[self.orderData.length - 1].properties.orderno;
                        self.selectedOrderID = self.orderData[self.orderData.length - 1].id
                        self.getProductioSchedule(self.selectedOrderID);
                    }
                } else {
                    notify(response.errMsg, 'error')
                }
                self.loading = false;
            })
        },
        //获取批次计划ID
        getProductioSchedule: function (selectedOrderID) {
            var self = this;
            self.loading = true;
            var param = {
                "appName": "mes",
                "sql": "select * from MES_ORDERSHEETPRODUCT where A_OBJID = " + selectedOrderID
            };
            self.common_fn.sqlFn(param, function (response) {
                if (response.resFlag == 0) {
                    self.selectedProductioScheduleID = parseInt(response.result[0].properties.b_objid)
                    self.getAssignsheets(self.selectedProductioScheduleID)
                } else {
                    notify(response.errMsg, 'error')
                }
            })
        },

        //获取已派工到班组的派工单，准备派工到个人
        getAssignsheets: function (selectedProductioScheduleID) {
            var self = this;
            self.loading = true;
            var param = {
                "filters": [{
                    "classFlag": 1,
                    "classType": "MesAssignsheet",
                    "propName": "state",
                    "op": 4,
                    "propValue": "0",      //已排产发布的订单
                    "propValue2": "",
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                }, {
                    "classFlag": 1,
                    "classType": "MesAssignsheet",
                    "propName": "PRODUCT_OBJID",
                    "op": 4,
                    "propValue": selectedProductioScheduleID + '',
                    "propValue2": "",
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                }],
                "orderItems": [],
                "queryclasses": ["MesProductionSchedule", "PlmPart", "MesTeam", "SysPerson", "QcpProcess", "MesSemiFinished", "SysSnapshot"],
                "queryFields": [{
                    "classType": "MesPartSemiFinished",
                    "propName": "IndexNo"
                }],
                "isQueryPreNext": true,
                "isQueryNextConsiderParent": true,
                "user": self.userinfo.user
            }
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.AssignSheetMgr", "QueryAssginSheetOneSet", param, self.userinfo, function (response) {
                if (response.resFlag == 0) {
                    self.assignsheetsLength = response.result.items.length;
                    for (var i = 0; i < response.result.items.length; i++) {
                        response.result.items[i]['properties'] = {};
                        for (var k = 0; k < response.result.items[i].itemObjects.length; k++) {
                            response.result.items[i].properties[response.result.items[i].itemObjects[k].type] = response.result.items[i].itemObjects[k].properties;
                            response.result.items[i].properties[response.result.items[i].itemObjects[k].type]['id'] = response.result.items[i].itemObjects[k].id;
                        }
                    }
                    for (var i = 0; i < response.result.items.length; i++) {
                        self.assignsheets.push({
                            "syssnapshot_id": response.result.items[i].properties.syssnapshot.id,
                            "mesproductionschedule_id": response.result.items[i].properties.mesproductionschedule.id,
                            "mesassignsheet_id": response.result.items[i].properties.mesassignsheet.id,
                            "productlayer": response.result.items[i].properties.messemifinished.productlayer,  //生产层
                            "worker": "",
                            "equipment": "",
                            "materialno": response.result.items[i].properties.plmpart.materialno,
                            "identityno": response.result.items[i].properties.plmpart.identityno,
                            "plmpartname": response.result.items[i].properties.plmpart.name,  //零件名称
                            "processno": response.result.items[i].properties.qcpprocess.processno,  //工序号
                            "qcpname": response.result.items[i].properties.qcpprocess.name,//工序名称
                            "startdate": response.result.items[i].properties.messemifinished.startdate,
                            "enddate": response.result.items[i].properties.messemifinished.enddate,
                            "orderquantity": response.result.items[i].properties.messemifinished.orderquantity,
                        })
                    }
                }
                self.loading = false;
            })
        },

        //选择某个订单
        selectOrder: function () {
            this.getProductioSchedule(this.selectedOrderID)
        },


        //派工
        assign: function (index, row) {
            if (row.worker == '' || row.equipment == '') {
                message('请选择派工人员和加工设备！', 'error');
            } else {
                this.claimworkDialog = true;
                this.assign_index = index;
                this.assign_row = row;
                this.claimWork.startNumber = row.orderquantity
            }
        },


        //派工到人，到设备
        release: function () {
            if (this.claimWork.startNumber == 0) {
                message('开工数量不能为0', 'error');
                return
            } else {
                //Step1 发布
                var self = this;
                self.claimworkLoading = true;
                var param = {
                    "user": self.userinfo.user,
                    "teamAssignSheet": {
                        "id": Number(self.assign_row.mesassignsheet_id),
                        "properties ": {
                            equipment_objid: self.assign_row.equipment
                        },
                        "type": "MesAssignSheet"
                    },
                    "snapshot": null,
                    "lstAssignData": [{
                        "id": 0,
                        "properties": {
                            workratio: 1,
                            person_objid: self.assign_row.worker
                        },
                        "type": 'MesAssignSheet'
                    }]
                };
                var mesproductionschedule = {
                    id: Number(self.assign_row.mesproductionschedule_id),
                    properties: {},
                    type: 'MesProductionSchedule'
                }
                self.common_fn.queryRelatedGraph(mesproductionschedule, 'MesProductionScheduleInSnapshot', self.userinfo, function (response) {
                    if (response.resFlag == 0) {
                        param.snapshot = response.result[0].entity;
                        self.common_fn.invoke("JridgeMesSTD_AdditionalService.dll", "JridgeMesSTD_AdditionalService.ProductionScheduleMgr", "TeamPlanPublish", param, self.userinfo, function (res1) {
                            if (res1.resFlag == 0) {
                                self.claimwork(res1.result[0].id);
                            } else {
                                notify(response.errMsg, 'error');
                                self.claimworkDialog = false
                            }
                        })
                    }
                })
            }
        },

        // 普通开工
        claimwork: function (id) {
            var self = this;
            var param = {
                assignmentSheets: [],
                reportSheets: [],
                user: self.userinfo.user
            };
            param.assignmentSheets.push({
                id: Number(id),
                type: "MesAssignSheet"
            });
            var reportsheet = {
                id: 0,
                type: "MesReportSheet",
                properties: {
                    ActualBeginQuantity: self.claimWork.startNumber,//开工数量
                    equipment_objid: self.assign_row.equipment
                }
            };
            param.reportSheets.push(reportsheet);
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.AssignSheetMgr", "StartAssignSheetDirectly", param, self.userinfo, function (response) {
                if (response.resFlag == 0) {
                    self.assignsheets.splice(self.assign_index, 1);
                    self.assignsheetsLength = self.assignsheetsLength - 1;
                    self.claimworkDialog = false;
                    if (self.assignsheetsLength == 0) {
                        //state 置为2 ，该订单已全部开工
                        var param = {
                            "appName": "mes",
                            "sql": "update MES_OrderSheet set state = 2 where objid = " + self.selectedOrderID
                        }
                        self.common_fn.sqlFn(param, function (response) {
                            if (response.resFlag == 0) {
                                message('该订单已完成全部派工！', 'success');
                            } else {
                                notify(response.errMsg, 'error')
                            }
                        })
                    } else {
                        message('派工成功！', 'success');
                    }
                } else {
                    notify(response.errMsg, 'error');
                }
                self.claimworkLoading = false;
                self.claimworkDialog = false
            })
        }
    },


    mounted: function () {
        var self = this;
        self.common_fn = new common_fn();
        self.common_fn_school = new common_fn_school();
        self.common_fn.init(function () {
            self.loading = true;
            self.common_fn.UserInfo(function (response) {
                if (response.user == null) {
                    window.location.href = 'login.html?redirect=claimWork.html';
                }
                self.userinfo = response;
                self.common_fn.CheckFunction(self.userinfo, 'mes', 'mes', 'WorkShopPlanMgr', '', '', 'workshopplan', function () { })
                //查询所在班组
                self.common_fn.PersonInTeam(self.userinfo.user.id, function (response) {
                    if (response.resFlag == 0) {
                        self.team = response.result[0];
                        //获取班组人员和设备
                        var entryObjects = [{
                            "id": self.team.id,
                            "type": "MesTeam"
                        }]
                        self.peopleByTeam = []; //清空
                        self.common_fn_school.QueryObjectsGraph(self.userinfo.user, null, entryObjects, ["MesTeamMember"], function (response) {
                            if (response.resFlag == 0) {
                                for (var i = 0; i < response.result.roots[0].children.length; i++) {
                                    self.peopleByTeam.push({
                                        "id": response.result.roots[0].children[i].entity.id,
                                        "name": response.result.roots[0].children[i].entity.properties.name
                                    })
                                }
                            }
                        })
                        self.devicesByTeam = [];
                        self.common_fn_school.QueryObjectsGraph(self.userinfo.user, null, entryObjects, ["MesTeamEquipment"], function (response) {
                            if (response.resFlag == 0) {
                                for (var i = 0; i < response.result.roots[0].children.length; i++) {
                                    self.devicesByTeam.push({
                                        "id": response.result.roots[0].children[i].entity.id,
                                        "name": response.result.roots[0].children[i].entity.properties.name
                                    })
                                }
                            }
                        })
                    } else {
                        notify(res.errMsg, 'error')
                    }
                })
                self.getOrders();
                self.loading = false;
            })
        });
    }
})

window.onload = function () {
    if (vm.mainTable_maxHeight != null || vm.mainTable_maxHeight != undefined) {
        window.setTimeout(function () {
            vm.mainTable_maxHeight = $(window).height() - 150;
        }, 100)
    }
}